package com.zhang.config;

import cn.dev33.satoken.stp.StpInterface;

import com.zhang.system.service.IRoleMenuService;
import com.zhang.system.entity.Role;
import com.zhang.system.service.IUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限加载接口实现类
 */
@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
@Slf4j
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IRoleMenuService roleMenuService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> permissionCode = roleMenuService.getPermissionCodeByUserId(Long.valueOf(String.valueOf(loginId)));
        log.info("你所具备的权限码{}", permissionCode);
        return permissionCode;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 本 list 仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        List<Role> userRoleInfo = userRoleService.getUserRoleInfo(Long.valueOf(String.valueOf(loginId)));
        List<String> collect = userRoleInfo.stream().map(Role::getRoleKey).collect(Collectors.toList());
        return collect;
    }

}

